package ModelLayer;

import java.util.ArrayList;



/**
 * EmloyeeCon is the container for Employee objects, implemented as a singleton.
 * 
 * @author anna
 * 
 * @version 1.0
 */
public class EmployeeCon {
    private ArrayList<Employee> employees;
    private static EmployeeCon instance;
    private Employee currentEmployee;

    /**
     * Private constructor of the single EmployeeCon object.
     */
    private EmployeeCon() {
        employees = new ArrayList<>();
    }

    /**
     * Public method to get an instance of the Employee object.
     * 
     * @return instance The single EmployeeCon object.
     */
    public static EmployeeCon getInstance() {
        if (instance == null) {
            instance = new EmployeeCon();
        }
        return instance;
    }

    /**
     * Adds an Employee object to the collection.
     * 
     * @param  An Employee object.
     * 
     * @throws Exception if the Employee object already is added to the collection.
     */
    public void addEmployee(Employee e) throws Exception {
    	if (!employees.contains(e)){
        	employees.add(e);
    	}
    	else throw new Exception ("Medarbejderen er allerede tilføjet.");
    }
    

    /**
     * Returns an Employee object found by its id-number.
     * 
     * @param employeeNo The id-number of the employee.
     * 
     * @return An Employee object.
     */
    public Employee getEmployee(int employeeNo){
        Employee e = null;
        boolean found = false;
        for (int index = 0; index < employees.size() && !found; ++index) {
            if (employees.get(index).getEmployeeNo() == employeeNo){
                e = employees.get(index);
                found = true;
            }
        }
        if (e != null){
            return e;
        }
        else {
            throw new IllegalArgumentException("Medarbejderen er ikke fundet.");    
        }
    }

    /**
     * Removes an Employee object from the collection found by its id-number.
     * 
     * @param employeeNo The id-number of the employee.
     */
    public void deleteEmployee(int employeeNo){
        employees.remove(getEmployee(employeeNo));
    }
    
    /**
     * Returns the value of the variable currentEmployee.
     * 
     * @return An Employee object.
     */
	public Employee getCurrentEmployee() {
		return currentEmployee;
	}
	
	/**
	 * Sets the value of the variable currentEmployee.
	 * 
	 * @param currentEmployee An employee object.
	 */
	public void setCurrentEmployee(Employee currentEmployee) {
		this.currentEmployee = currentEmployee;
	}
    

}

